package initialize

import (
	docs "AsyncFlashStream/doc"
	"AsyncFlashStream/global"
	"AsyncFlashStream/middleware"
	"AsyncFlashStream/router"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"net/http"
)

// Routers
// @Author Cat
// @Description: gin初始化路由
func Routers() *gin.Engine {
	server := gin.New()
	server.Use(gin.Recovery())
	if gin.Mode() == gin.DebugMode {
		server.Use(gin.Logger())
	}
	//跨域处理
	server.Use(middleware.CorsByRules())

	//业务路由
	// 方便统一添加路由组前缀 多服务器上线使用
	PublicGroup := server.Group(global.G_CONFIG.System.RouterPrefix) //公共无鉴权路由
	{
		// 健康监测
		PublicGroup.GET("/health", func(c *gin.Context) {
			c.JSON(http.StatusOK, "ok")
		})
	}
	{
		router.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权
	}
	//注册接口文档
	docs.SwaggerInfo.BasePath = global.G_CONFIG.System.RouterPrefix
	server.GET(global.G_CONFIG.System.RouterPrefix+"/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	global.G_LOG.Info("register swagger handler")

	global.G_LOG.Info("router register success")
	return server
}
